<?php

namespace api\agent;

use api\lib\ErrorLog;
use api\lib\Http;

/**
 * https://www.gname.com/domain/api/dropcatch/list
 * 成为经销商可以取得app_id, app_key
 * https://www.gname.com/agent
 */
class GName
{
    use ErrorLog;

    /** @var string */
    private $appId;
    /** @var string */
    private $appKey;

    const PREFIX = "https://api.gname.com";
    const API_DROP_CATCH = "/api/dropcatch/list";

    public function __construct(/* string */ $appId, /* string */ $appKey)
    {
        $this->appId = $appId;
        $this->appKey = $appKey;
    }

    public function dropCatch(/* string */$date, /* int */ $page = 1, /*int */ $size = 1000)
    {
        // header("Content-Type: application/json");

        $data = array();
        $data['appid'] = $this->appId;
        $data['gntime'] = time();
        $data['deletion_date'] = $date;
        $data['page'] = $page;
        $data['deletion_type'] = '1';
        $data['pagesize'] = $size;
        $gntoken = self::md5Sign(Http::buildQuery($data), $this->appKey);
        $data['gntoken'] = $gntoken;
        $requestUrl = self::PREFIX . self::API_DROP_CATCH;

        $http = new Http();
        $s = $http->post($requestUrl, $data);
        $ret = json_decode($s, true);
        if ($ret['code'] < 0) {
            $this->writeLog($s);
        }
        return $ret['data'];
    }

    private static function md5Sign(/* string */ $prestr, /* string */ $key)
    {
        $signstr = $prestr . $key;
        return strtoupper(md5($signstr));
    }

//sign verify
    private static function md5Verify(/* string */ $prestr, /* string */ $sign, /* string */ $key)
    {
        $mysgin = self::md5Sign($prestr, $key);

        if ($mysgin == $sign) {
            return true;
        } else {
            return false;
        }
    }
}